#!/bin/bash

if test $# != 2; then
    die "Error: Usage $(basename $0) file %-encoded-data"
fi

if ! echo "$2"|grep -e "^\(%[a-f0-9][a-f0-9]\)*$" -q; then
    die 'Format error: $2 not regexp match to "^\(%[a-f0-9][a-f0-9]\)*$"'
fi

set -e
mkdir -p "$(dirname /factory/$1.txt)"
echo $2 | awk '{
    n = split($1, nums, "%");
    for(i=2; i<=n; i++) {
        a="0x"nums[i];
        printf "%c", a + 0
    }
}' > "/factory/$1.txt"
